<template>
  <div class="container">
      <div class="regist">注册账号</div>
    <el-form :label-position="labelPosition" :rules="rules"  class="form" ref="formData" label-width="80px" :model="formData">
        <el-form-item label="账号" prop="username">
            <el-input v-model="formData.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
            <el-input v-model="formData.password"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="phone_num">
            <el-input v-model="formData.phone_num"></el-input>
        </el-form-item>
        <el-button type="primary" class="btn" @click="submitForm('formData')">注册</el-button>
    </el-form>
  </div>
</template>

<script>
import { post } from '../assets/http'
export default {
  name: 'Regist',
  data () {
    return {
      labelPosition: 'right',
      formData: {
        username: '',
        password: '',
        phone_num: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 3, max: 12, message: '长度在 3 到 12 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 12, message: '长度在 6 到 12 个字符', trigger: 'blur' }
        ],
        phone_num: [
          { required: true, message: '请输入电话', trigger: 'blur' },
          { min: 11, max: 11, message: '请检查电话号码', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    submitForm (form) {
      this.$refs[form].validate(async (valid) => {
        if (valid) {
          var res = await post('regist/store', this.formData)
          if (res.flag) {
            this.$message({
              message: '注册成功',
              type: 'success'
            })
            this.$router.push({path: '/'})
          } else {
            this.$message.error('该账号已被注册')
          }
        }
      })
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.container {
    width: 30%;
    margin-left: 35%;
    margin-top: 10%;
}
.btn {
    margin-left: 200px;
}
.regist {
    color: rgb(83, 149, 236);
    font-size: 50px;
    margin-left: 150px;
    margin-top: -100px;
    margin-bottom: 50px;
}
</style>
